package skipClass.java;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;

import skipClass.java.ExamQuestion;

public class ExamQuestionReader
{
	private final static String TAG = "ClassSkippers";
	private ArrayList<ExamQuestion> Questions;
	
	private Context context;
	
	private String FileName;
	private boolean fromFile = false;
	
	private int ResourceId;
	private boolean fromResource = false;
	
	public ExamQuestionReader(Context con, String fileName)
	{
		context = con;
		FileName = fileName;
		fromFile = true;
	}
	
	public ExamQuestionReader(Context con, int resourceId)
	{
		context = con;
		ResourceId = resourceId;
		fromResource = true;
	}
	
	public ArrayList<ExamQuestion> readXmlFile()
	{
		Questions = new ArrayList<ExamQuestion>();
		
		DocumentBuilder docBuilder;
		
		try 
		{
			docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		}
		catch (ParserConfigurationException e) 
		{
			Log.e(TAG, "Could not get instance of DocumentBuilder");
			return null;
		}
		
		InputStream is;
		
		if (fromFile)
		{
			File file = new File(FileName);
		
			if (!file.canRead())
			{
				Log.e(TAG, "Can not read file " + FileName);
				return null;
			}
		
			//FileInputStream fis;
		
			try
			{
				is = new FileInputStream(file);
			}
			catch (FileNotFoundException e)
			{
				Log.e(TAG, "Can not create input stream from " + FileName);
				return null;
			}
		}
		
		else if (fromResource)
		{
			is = context.getResources().openRawResource(ResourceId);
		}
		
		else
		{
			Log.e(TAG, "No valid input method defined");
			return null;
		}
		
		Document doc;
		
		try
		{
			doc = docBuilder.parse(is);
		}
		catch (SAXException e)
		{
			Log.e(TAG, "Can not read XML data from " + FileName);
			return null;
		}
		catch (IOException e)
		{
			Log.e(TAG, "Can not read XML data from " + FileName);
			return null;
		}
		
		NodeList QuestionNodes = doc.getElementsByTagName("Question");
		
		Log.i(TAG, QuestionNodes.getLength() + " questions found.");
		
		for (int i = 0; i < QuestionNodes.getLength(); i++)
		{
			Node Question = QuestionNodes.item(i);
			
			NodeList QuestionValues = Question.getChildNodes();
			
			String question = null;
			
			ArrayList<String> bogusAnswers = new ArrayList<String>();
			
			String realAnswer = null;
			
			Log.i(TAG, QuestionValues.getLength() + "children found in question " + i + ".");
			
			for (int j = 0; j < QuestionValues.getLength(); j++)
			{
				Node QuestionValue = QuestionValues.item(j);
				
				if (QuestionValue.getNodeType() == Node.ELEMENT_NODE)
				{
					if (QuestionValue.getNodeName().equals("QuestionText"))
					{
						question = new String(QuestionValue.getFirstChild().getNodeValue());
					}
					
					else if (QuestionValue.getNodeName().equals("BogusAnswerText"))
					{
						bogusAnswers.add(new String(QuestionValue.getFirstChild().getNodeValue()));
					}
					
					else if (QuestionValue.getNodeName().equals("RealAnswerText"))
					{
						realAnswer = new String(QuestionValue.getFirstChild().getNodeValue());
					}
					
					else
					{
						Log.w(TAG, "Unidentified tag name " + QuestionValue.getNodeName() + " in file.");
					}
				}
				
				else
				{
					Log.w(TAG, "File contains a sub-entity " + QuestionValue.getNodeName() + " that is not an element");
				}
			}
			
			if ((question != null) && (!bogusAnswers.isEmpty()) && (realAnswer != null))
				Questions.add(new ExamQuestion(question, bogusAnswers, realAnswer));
		}
		
		logQuestions();
		
		return Questions;
	}
	
	private void logQuestions()
	{
		for (int i = 0; i < Questions.size(); i++)
		{
			Log.i(TAG, "Question " + (i +1));
			
			ExamQuestion question = Questions.get(i);
			
			Log.i(TAG, "Question text: " + question.getQuestion());
			Log.i(TAG, "Answer: " + question.getRealAnswer());
			
			ArrayList<String> bogusAnswers = question.getBogusAnswers();
			
			for (int j = 0; j < bogusAnswers.size(); j++)
			{
				Log.i(TAG, "Bogus Answer: " + bogusAnswers.get(j));
			}
			
		}
	}
	
}
